//
//  CountDownUtils.m
//  GomeMarketingClient
//
//  Created by Kelly xu on 2017/3/16.
//  Copyright © 2017年 GomeFinance. All rights reserved.
//

#import "CountDownUtils.h"

@implementation CountDownUtils
+(void)checkCodeWithBtn:(id)sender buttonColor:(UIColor*)buttonColor buttonHighlightedColor:(UIColor*)highColor timer:(dispatch_source_t)timer handle:(void(^)())handle{
    UIButton *button=(UIButton*)sender;
    __block int timeout=60; //倒计时时间
    dispatch_source_set_timer(timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                button.enabled=YES;
                [button setTitle:@"获取验证码" forState:UIControlStateNormal];
                [button setTitleColor:buttonColor forState:UIControlStateNormal];
            });
        }else{
            //int minutes = timeout / 60;
            int seconds = timeout;
            NSString *strTime = [NSString stringWithFormat:@"重新获取(%02d)",seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [button setTitleColor:highColor forState:UIControlStateNormal];
                button.enabled=NO;
                [button setTitle:strTime forState:UIControlStateNormal];
                if (seconds==59) {
                    //这里写处理的代码
                    handle();
                }
            });
            timeout--;
            
        }
    });
    dispatch_resume(timer);
}
+(void)checkCodeWithBtn:(UILabel*)timeLab leftSecs:(NSInteger)seconds timer:(dispatch_source_t)timer handle:(void(^)())handle{
    __block int timeout = seconds; //倒计时时间
    dispatch_source_set_timer(timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                handle();
            });
        }else{
            //int minutes = timeout / 60;
            int seconds = timeout;
            int minutes = seconds/60;
            int sec = seconds - minutes*60;
            NSString *strTime = [NSString stringWithFormat:@"时间倒计时：%02d:%02d",minutes,sec];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                timeLab.text = strTime;
            });
            timeout--;
            
        }
    });
    dispatch_resume(timer);
}
+(void)timeCounterWithLab:(UILabel*)timeLab leftSecs:(NSInteger)seconds timer:(dispatch_source_t)timer perSecHandle:(void(^)())perhandle handle:(void(^)())handle{
    __block NSInteger timeout = 0; //倒计时时间
    __block NSInteger currentTime = seconds;
    dispatch_source_set_timer(timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(timer, ^{
        if(currentTime==timeout){ //倒计时结束，关闭
            dispatch_source_cancel(timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                handle();
            });
        }else{
            //int minutes = timeout / 60;
            NSInteger seconds = currentTime;
            NSInteger minutes = seconds/60;
            NSInteger sec = seconds - minutes*60;
            NSString *strTime = [NSString stringWithFormat:@"%02ld:%02ld",minutes,sec];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                NSLog(@"_____________________%@",strTime);
                timeLab.text = strTime;
            });
            currentTime--;
            if (currentTime%60==0) {
                perhandle();
            }
        }
    });
    dispatch_resume(timer);
}
@end
